<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>EASY后台-用户</title>
    <link rel="icon" th:href="@{/static/images/favicon.ico}">
    <link rel="stylesheet" th:href="@{/static/css/common.css}">
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
</head>
<body>
<!-- 页面内容 -->
<div class="layui-fluid">
    <div class="layui-row layui-col-space10">
        <div class="layui-col-xs3">
            <div class="layui-card">
                <div class="layui-card-body">
                    <!-- 表格 -->
                    <table class="layui-hide" id="deptTable" lay-filter="deptTable"></table>
                </div>
            </div>
        </div>
        <div class="layui-col-xs9">
            <div class="layui-card">
                <div class="layui-card-body">
                    <!-- 搜索条件 -->
                    <form class="layui-form">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">用户名</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="username" placeholder="请输入" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">昵称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="nickname" placeholder="请输入" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn layui-btn-md" lay-submit lay-filter="tableSearch">
                                    <i class="layui-icon layui-icon-search"></i>
                                    查询
                                </button>
                                <button type="reset" class="layui-btn layui-btn-primary layui-btn-md">
                                    <i class="layui-icon layui-icon-refresh"></i>
                                    重置
                                </button>
                            </div>
                        </div>
                    </form>
                    <!-- 表格 -->
                    <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

<!-- 表格工具 -->
<script type="text/html" id="tableToolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">
            <i class="layui-icon layui-icon-addition"></i>新建
        </button>
        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>删除
        </button>
    </div>
</script>

<!-- 表格行工具 -->
<script type="text/html" id="tableRowTool">
    <a class="layui-btn layui-btn-checked layui-btn-xs" lay-event="auth">授权</a>
    <a class="layui-btn layui-btn-checked layui-btn-xs" lay-event="resetPwd">重置密码</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="remove">删除</a>
</script>

<script th:src="@{/static/js/jquery.js}"></script>
<script th:src="@{/static/layui/layui.js}"></script>
<script>
    layui.use(['element', 'layer', 'table', 'treeTable', 'laydate', 'form'], function () {
        var element = layui.element;
        var layer = layui.layer;
        var table = layui.table;
        var treeTable = layui.treeTable;
        var laydate = layui.laydate;
        var form = layui.form;

        var deptId = null;
        var deptTableObj = treeTable.render({
            elem: '#deptTable',
            url: '/sysDept/list',
            method: 'GET',
            tree: {
                customName: {
                    name: 'deptName'
                }
            },
            defaultToolbar: [],
            cols: [
                [
                    {field: 'deptName', title: '所属部门', align: 'left'},
                ]
            ],
            page: false,
            loading: true,
            skin: 'nob', // 边框风格
            size: 'md', // 尺寸
            before: function (options) { // 数据渲染之前的回调函数

            },
            error: function (e, msg) { // 数据请求失败的回调函数

            },
            parseData: function (res) { // 数据格式解析的回调函数
                return {
                    "code": res.code === 200 ? 0 : res.code,
                    "msg": res.msg,
                    "data": res.data
                }
            },
        });

        // 行单击事件
        table.on('row(deptTable)', function(obj) {
            var data = obj.data;
            deptId = data.id;

            tableObj = renderTable();
        });

        // table渲染
        var tableObj = renderTable();
        function renderTable() {
            return table.render({
                elem: '#dataTable',
                url: '/sysUser/page',
                method: 'GET',
                request: {
                    pageName: 'pageNum',
                    limitName: 'pageSize'
                },
                where: { // 添加额外条件
                    deptId: deptId
                },
                autoSort: false,
                toolbar: '#tableToolbar',
                defaultToolbar: ['filter', 'exports', 'print'],
                cols: [
                    [
                        {type: 'checkbox', fixed: 'left'},
                        {type: 'numbers', title: '序号', align: 'center'},
                        {
                            field: 'avatar', title: '头像', width: 100, align: 'center',
                            templet: function (row) {
                                return '<img src="/sysFile/download/' + row.avatar + '" style="width: 30px;height: 30px;">';
                            }
                        },
                        {field: 'username', title: '用户名', width: 120, align: 'center', sort: true},
                        {field: 'nickname', title: '昵称', width: 120, align: 'center'},
                        {field: 'deptName', title: '所属部门', width: 150, align: 'center'},
                        {
                            field: 'gender', title: '性别', width: 80, align: 'center', sort: true,
                            templet: function (row) {
                                return row.genderDesc;
                            }
                        },
                        {field: 'mobile', title: '手机号', width: 150, align: 'center'},
                        {field: 'address', title: '联系地址', width: 150, align: 'center'},
                        {field: 'remark', title: '备注', width: 120, align: 'center'},
                        {field: 'createTime', title: '创建时间', width: 200, align: 'center'},
                        {fixed: 'right', title: '操作', minWidth: 240, align: 'center', templet: '#tableRowTool'}
                    ]
                ],
                page: {
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'], // 自定义分页布局
                    limit: 10,
                    limits: [5, 10, 20, 50, 100],
                    curr: 1,
                    groups: 5, // 只显示5个连续页码
                    first: false,
                    last: false
                },
                loading: true,
                skin: 'grid', // 边框风格
                size: 'md', // 尺寸
                even: true, // 隔行背景
                before: function (options) { // 数据渲染之前的回调函数

                },
                done: function (res, curr, count, origin) { // 数据渲染完毕的回调函数

                },
                error: function (e, msg) { // 数据请求失败的回调函数

                },
                parseData: function (res) { // 数据格式解析的回调函数
                    return {
                        "code": res.code === 200 ? 0 : res.code,
                        "msg": res.msg,
                        "count": res.data.total,
                        "data": res.data.results
                    }
                },
            });
        }

        // 触发排序事件
        table.on('sort(dataTable)', function (obj) {
            table.reload('dataTable', {
                initSort: obj,
                where: {
                    prop: obj.field,
                    order: obj.type
                }
            });
        });

        // 监听表格工具条
        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') {
                layer.open({
                    type: 2,
                    area: ['500px', '450px'],
                    title: '新建用户',
                    shadeClose: true,
                    scrollbar: false,
                    content: '/sysUser/addForm'
                });
            }
            if (obj.event === 'batchRemove') {
                var checkRows = table.checkStatus('dataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请先选择数据', {icon: 0});
                    return;
                }
                layer.confirm('确定删除所选数据吗？', {
                    btn: ['确定', '关闭']
                }, function () {
                    var ids = checkRows.data.map(function (row) {
                        return row.id;
                    });
                    $.ajax({
                        url: '/sysUser/remove',
                        type: 'DELETE',
                        contentType: 'application/json',
                        dataType: 'json',
                        data: JSON.stringify({idList: ids}),
                        success: function (response) {
                            if (response.code === 200) {
                                layer.msg('删除成功', {icon: 1});
                                tableObj.reload();
                            } else {
                                layer.msg(response.msg, {icon: 0});
                            }
                        },
                        error: function (xhr, status, error) {
                            layer.msg('请求失败', {icon: 0});
                        }
                    });
                });
            }
        });

        // 监听表格行工具
        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'auth') {
                layer.open({
                    type: 2,
                    area: ['500px', '300px'],
                    title: '授权角色',
                    shadeClose: true,
                    scrollbar: false,
                    content: '/sysUserRole/authForm/' + obj.data.id
                });
            }
            if (obj.event === 'resetPwd') {
                $.ajax({
                    url: '/sysUser/resetPwd/' + obj.data.id,
                    type: 'PUT',
                    contentType: 'application/json',
                    dataType: 'json',
                    success: function (response) {
                        if (response.code === 200) {
                            layer.msg('重置密码成功', {icon: 1});
                            tableObj.reload();
                        } else {
                            layer.msg(response.msg, {icon: 0});
                        }
                    },
                    error: function (xhr, status, error) {
                        layer.msg('请求失败', {icon: 0});
                    }
                });
            }
            if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    area: ['500px', '450px'],
                    title: '编辑用户',
                    shadeClose: true,
                    scrollbar: false,
                    content: '/sysUser/editForm/' + obj.data.id
                });
            }
            if (obj.event === 'remove') {
                layer.confirm('确定删除所选数据吗？', {
                    btn: ['确定', '关闭']
                }, function () {
                    $.ajax({
                        url: '/sysUser/remove',
                        type: 'DELETE',
                        contentType: 'application/json',
                        dataType: 'json',
                        data: JSON.stringify({idList: [obj.data.id]}),
                        success: function (response) {
                            if (response.code === 200) {
                                layer.msg('删除成功', {icon: 1});
                                tableObj.reload();
                            } else {
                                layer.msg(response.msg, {icon: 0});
                            }
                        },
                        error: function (xhr, status, error) {
                            layer.msg('请求失败', {icon: 0});
                        }
                    });
                });
            }
        });

        // 监听查询
        form.on('submit(tableSearch)', function (data) {
            table.reload('dataTable', {where: data.field}, true);
            return false;
        });
    });
</script>
</html>